from typing import List


class ListNode:
    def __init__(self, x):
        self.val = x
        self.next = None

def swapNodeInPairsRec(head: ListNode)->ListNode:
    if not head or not head.next:
        return head
    first = head
    second = head.next
    first.next = swapNodeInPairsRec(second.next)
    second.next = first
    return second

def swapNodeInPairsLoop(head: ListNode)->ListNode:
    dummy_head = ListNode(0)
    dummy_head.next = head

    cur = dummy_head
    while cur.next and cur.next.next:
        first = cur.next
        second = cur.next.next
        first.next = second.next
        second.next = first
        cur.next = second
        cur = cur.next.next

    return dummy_head.next